
//
//  FZPicsCollectionView.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/4/6.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

class FZPicsCollectionView: UICollectionView {

    var cameraClosure : (()->())?
    
    lazy var imageArr : [UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        
        let margin : CGFloat = 5
        
        let edgeMargin : CGFloat = 10
        
        let itemWith = (screenWidth - edgeMargin * 2 - margin * 2)/3
        
        layout.itemSize = CGSize(width: itemWith, height: itemWith)
        
        layout.minimumLineSpacing = margin
        
        layout.minimumInteritemSpacing = margin
        
        super.init(frame: frame, collectionViewLayout: layout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
       self.backgroundColor = UIColor.white
       self.isHidden = true
       self.dataSource = self
       self.delegate = self
       register(FZPictureCell.self, forCellWithReuseIdentifier: picsColletionCell)
    }
}

extension FZPicsCollectionView : UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.item == imageArr.count {
            //执行闭包
            cameraClosure?()
        }
    }
    
    //添加图片到数组中
    func addImageToArr(picImg: UIImage) {
        self.isHidden = false
        if imageArr.count >= 9 {
            return
        }
        
        imageArr.append(picImg)
        reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageArr.count >= 1 && imageArr.count < 9{
             return imageArr.count + 1
        }
        return imageArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: picsColletionCell, for: indexPath) as! FZPictureCell
      cell.backgroundColor = UIColor.yellow
        
        if indexPath.item == imageArr.count {
            cell.image = nil
            
        }else{
            
            cell.image = imageArr[indexPath.item]
            cell.indexPath = indexPath
            cell.deleIndexClosure = {[weak self] (index : Int)->() in
                self?.imageArr.remove(at: index)
                self?.reloadData()
            }
        }
        return cell
    }
    
}
class FZPictureCell: UICollectionViewCell {
    
    var indexPath : IndexPath?
    var deleIndexClosure : ((Int)->())?
    var image : UIImage? {
        
        didSet{
            if image == nil {
                
                imgView.image = UIImage(named: "compose_pic_add")
                deleteButton.isHidden = true
            }else{
                 deleteButton.isHidden = false
                 imgView.image = image
            }
           
        }
    }
    
    fileprivate lazy var imgView : UIImageView = UIImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    fileprivate lazy var deleteButton : UIButton = {
        
    let btn = UIButton()
        
      btn.setImage(UIImage(named: "compose_photo_close"), for: .normal)
      btn.addTarget(self, action: #selector(deleteImage), for: .touchUpInside)
    
      return btn
    }()
    
    
    func setupUI() {
       
        contentView.addSubview(imgView)
        contentView.addSubview(deleteButton)
        deleteButton.snp.makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
        imgView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
    func deleteImage() {
        //删除图片,执行闭包回调
        if let index = indexPath {
            
            deleIndexClosure?(index.item)

        }
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}









